package cn.phub.pet.service.impl;

import cn.phub.basic.service.impl.BaseServiceImpl;
import cn.phub.basic.util.BaiduAuditUtils;
import cn.phub.pet.domain.Pet;
import cn.phub.pet.domain.PetDetail;
import cn.phub.pet.domain.PetOnlineAuditLog;
import cn.phub.pet.mapper.PetDetailMapper;
import cn.phub.pet.mapper.PetMapper;
import cn.phub.pet.mapper.PetOnlineAuditLogMapper;
import cn.phub.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jijie
 * @since 2022-04-10
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

   @Autowired
   private PetMapper petMapper;

    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    @Override
    public void add(Pet pet) {
        //保存宠物基本信息--注意：需要保存shopId shopId来自于当前登录人信息
        petMapper.save(pet);//返回自增Id
        //保存宠物的详情信息
        PetDetail detail = pet.getDetail();
        if(detail != null){
            detail.setPetId(pet.getId());
            petDetailMapper.save(detail);
        }
    }

    @Override
    public void update(Pet pet) {
        super.update(pet);
    }

    @Override
    public void remove(Long id) {
        super.remove(id);
    }

    @Override
    public void onsale(List<Long> ids) {
        for (Long id : ids) {//每一次循环查一个宠物并审核它的信息
            Pet pet = petMapper.loadById(id);
            //审核信息是否合法
            StringBuffer sb = new StringBuffer();
            if(!BaiduAuditUtils.TextCensor(pet.getName())){
                sb.append("宠物:"+pet.getName() +"名字不合法!");
            }
           /* 审核信息是否合法
            if(!BaiduAuditUtils.ImgCensor("http://47.95.117.210:8888"+pet.getResources().split(",")[0])){
                sb.append("宠物:"+pet.getName() +"图片不合法");
            }*/
            if(sb.length() > 0){//审核失败，长度如果大于零就可以保存日志了
                //不合法记录审核失败日志，跳过然后执行下一次循环
                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setPetId(id);
                log.setState(0);
                log.setAuditTime(new Date());
                log.setNote(sb.toString());
                petOnlineAuditLogMapper.save(log);
                continue;
            }
            //到这里就说明审核没有问题可以上架了--修改状态--修改上架时间
            Map<String, Object> map = new HashMap<>();
            map.put("id",id);
            map.put("onSaleTime",new Date());
            petMapper.onSale(map);
        }
    }

    @Override
    public void offsale(List<Long> ids) {
        Map<String, Object> map = new HashMap<>();
        map.put("ids",ids);
        map.put("offSaleTime",new Date());
        petMapper.offSale(map);
    }
}
